<%@include file="connection.jsp" %>


<%
String hid=request.getParameter("hid").toString();
String sr=session.getAttribute("sr").toString();
int flag=0;

if(hid.equals("add")){
    String nm=request.getParameter("port").toString();
    st=con.createStatement();
    int i=st.executeUpdate("insert into portfolio values(NULL,'"+sr+"','"+nm+"')");
    
    st=con.createStatement();
    rs=st.executeQuery("select pf_id from portfolio where u_id='"+sr+"' and pf_nm='"+nm+"'");
    
    while(rs.next()){
        String pid=rs.getString(1);
        response.sendRedirect("portfolio.jsp?pid="+pid);
    }
}

else if(hid.equals("rename")){
    String pf=request.getParameter("pf").toString();
    String nm=request.getParameter("port1").toString();
    
    st=con.createStatement();
    int i=st.executeUpdate("update portfolio set pf_nm='"+nm+"' where pf_id='"+pf+"'");
    response.sendRedirect("portfolio.jsp?pid="+pf);
}
else if(hid.equals("del")){
    String pf=request.getParameter("pf").toString();
    
    st=con.createStatement();
    int i=st.executeUpdate("delete from portfolio where pf_id='"+pf+"'");
    int j=st.executeUpdate("delete from pf_detail where pf_id='"+pf+"'");
    
    st=con.createStatement();
    rs=st.executeQuery("select pf_id from portfolio where u_id='"+sr+"' limit 1");

    while(rs.next()){
        pf=rs.getString(1);
    }
    response.sendRedirect("portfolio.jsp?pid="+pf);
}

else if(hid.equals("stock")){
     String pf=request.getParameter("pf").toString();
     String com=request.getParameter("com_name");
     String quanti=request.getParameter("quanti");
     String price=request.getParameter("price");
     String date=request.getParameter("date");
     
     
     st=con.createStatement();
     rs=st.executeQuery("select com_id from company where ticker='"+com+"'");
     while(rs.next()){
         com=rs.getString(1);
        
     }
     if(rs.first()==false)
                {
                     flag=1;
        
     }
     
    
     st=con.createStatement();
     int i=st.executeUpdate("insert into pf_detail values(NULL,'"+pf+"','"+com+"','"+price+"','"+quanti+"','"+date+"')");
     response.sendRedirect("portfolio.jsp?pid="+pf);

             if (flag==1){
                  response.sendRedirect("portfolio.jsp?pid="+pf+"&msg=no");
             }
             else{
                 i=st.executeUpdate("insert into pf_detail values(NULL,'"+pf+"','"+com+"','"+price+"','"+quanti+"','"+date+"')");
                 response.sendRedirect("portfolio.jsp?pid="+pf);
             }
     
    

}
%>